package com.cx.algrithm.binary_tree.binary_search_tree.red_black_tree;

import java.util.Scanner;

/**
 * @Author: robert.song
 * @Date: Created in 2022/3/16
 */
public class RBTreeTest {

    public static void main(String[] args) {
        insertOpt();
    }

    public static void insertOpt() {
        Scanner scanner = new Scanner(System.in);
        RBTree<String, Object> rbt = new RBTree<>();
        boolean flag = true;
        while (flag) {
            System.out.println("请你输入要插入的节点");
            String key = scanner.next();
//            这里代码最多支持3位数，3位以上的话红黑树显示太错位了，这里就不重构了，大家可自行重构
            if (key.length() == 1) {
                key = "00" + key;
            } else if (key.length() == 2) {
                key = "0" + key;
            }
            rbt.put(key, null);
            TreeOperation.show(rbt.getRoot());
        }

    }

    public static void deleteOpt() {
        RBTree<String, Object> rbTree = new RBTree<>();
//        测试1：预先造10个节点（1-10）
        String keyA = null;
        for (int i = 1; i < 11; i++) {
            if ((i + "").length() == 1) {
                keyA = "00" + i;
            } else if ((i + "").length() == 2) {
                keyA = "0" + i;
            }
            rbTree.put(keyA, null);
        }
//        测试2：包含2位数和3位数的测试代码1 2 3 4 5 66 77 88 99 100 101
        rbTree.put("001", null);
        rbTree.put("002", null);
        rbTree.put("003", null);
        rbTree.put("004", null);
        rbTree.put("005", null);
        rbTree.put("066", null);
        rbTree.put("077", null);
        rbTree.put("088", null);
        rbTree.put("099", null);
        rbTree.put("100", null);
        rbTree.put("101", null);

        TreeOperation.show(rbTree.getRoot());
//        以下开始删除
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入你要删除的节点");
            String key = scanner.next();
            System.out.println();
//          这里代码最多支持3位数，3位数以上的话红黑树显示太错位了，这里就不重构了，大家可自行重构
            if (key.length() == 1) {
                key = "00" + key;
            } else if (key.length() == 2) {
                key = "0" + key;
            }
//          1 2 3 88 66 77 100 5 4 101
            rbTree.remove(key);
            TreeOperation.show(rbTree.getRoot());
        }
    }
}
